/*
 * 创建日期 2005-10-9
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorUtil;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class Validation {
	public static void checkRequired(
		String elementName,
		String property,
		Object value)
		throws ReportException {
		boolean bRequired = true;
		if (value == null) {
			bRequired = false;
		}
		if (value instanceof String) {
			bRequired = !GenericValidator.isBlankOrNull((String) value);
		}

		if (bRequired == false) {
			throw new ReportException(
				"Element["
					+ elementName
					+ "]:Attribute["
					+ property
					+ "] is required.");
		}
	}

	/**
	 * 如果value>base,则认为是有意义的
	 * @param elementName
	 * @param property
	 * @param value
	 * @param base
	 * @throws ReportException
	 */
	public static void checkRequired(
		String elementName,
		String property,
		int value,
		int base)
		throws ReportException {
		boolean bRequired = true;

		if (value <= base) {
			bRequired = false;
		}

		if (bRequired == false) {
			throw new ReportException(
				"Element["
					+ elementName
					+ "]:Attribute["
					+ property
					+ "] is required.");
		}
	}

	/**
	 * 校验数据不能为Null
	 * @param validatorObjs
	 * @param singleObj
	 * @param check_property
	 * @return
	 * @throws ReportException
	 */
	public boolean validatorRequired(
		Object validatorObjs,
		Object singleObj,
		String check_property)
		throws ReportException {
		
		String[] propertys = check_property.split(",");
		for(int i=0;i<propertys.length;i++){
			//取得属性值
			String value = ValidatorUtil.getValueAsString(singleObj,propertys[i]);
			//如果是空,校验失败
			if (GenericValidator.isBlankOrNull(value)){
				return false;
			}
		}
		
		return true;
	}
}
